package com.water.guider


import java.util.*

class AGuider(builder: Builder) {
    private var onStartListener: OnAGuiderStartListener?
    private var onStopListener: OnAGuiderStopListener?
    private val guiders: MutableList<Guider>?
    private var index = 0
    private fun init() {
        for (guider in guiders!!) {
            guider.addOnGuiderStartListener(MyOnGuiderStartListener())
            guider.addOnGuiderStopListener(MyOnGuiderStopListener())
        }
    }

    fun show(): AGuider {
        if (guiders != null && !guiders.isEmpty()) {
            guiders[index].show()
        }
        return this
    }

    /**
     * dismiss current GuiderView.
     */
    fun dismissGuider() {
        if (guiders != null && !guiders.isEmpty()) {
            if (index < guiders.size) {
                guiders[index].dismiss()
            }
        }
    }

    /**
     * dismiss all GuiderView.
     */
    fun dismiss() {
        if (guiders != null && !guiders.isEmpty()) {
            if (index < guiders.size) {
                val guider = guiders[index]
                guiders.clear()
                guider.dismiss()
            }
        }
    }

    val isVisible: Boolean
        get() {
            if (guiders != null && !guiders.isEmpty()) {
                if (index < guiders.size) {
                    return guiders[index].isVisible
                }
            }
            return false
        }

    inner class MyOnGuiderStartListener : OnGuiderStartListener {

        override fun onStart() {
            if (index == 0 && onStartListener != null) {
                onStartListener?.onStart()
            }
        }
    }

    inner class MyOnGuiderStopListener : OnGuiderStopListener {

        override fun onStop() {
            if (guiders != null) {
                if (++index < guiders.size) {
                    guiders[index].show()
                }
                if (index >= guiders.size && onStopListener != null) {
                    onStopListener?.onStop()
                }
            }
        }
    }

    class Builder {
        var onStartListener: OnAGuiderStartListener? = null


        var onStopListener: OnAGuiderStopListener? = null


        val guiders: MutableList<Guider> = ArrayList()

        fun addGuider(guider: Guider): Builder {
            guiders.add(guider)
            return this
        }

        fun addGuiders(vararg guiders: Guider): Builder {
            this.guiders.addAll(Arrays.asList(*guiders))
            return this
        }

        fun setOnAGuidertStartListener(listener: OnAGuiderStartListener?): Builder {
            onStartListener = listener
            return this
        }

        fun setOnAGuidertStopListener(listener: OnAGuiderStopListener?): Builder {
            onStopListener = listener
            return this
        }

        fun build(): AGuider {
            return AGuider(this)
        }

        fun show(): AGuider {
            val aGuider = build()
            aGuider.show()
            return aGuider
        }
    }

    companion object {
        val TAG = AGuider::class.java.simpleName
    }

    init {
        onStartListener = builder.onStartListener
        onStopListener = builder.onStopListener
        guiders = builder.guiders
        init()
    }
}